#include "../thirdparty/cJSON/cJSON.h"
#include "../thirdparty/LOG/log.h"

int main(int argc, char *argv[])
{
    char *name = "baijiahao";
    int age = 18;

    //生成字符串
    //创建一个空的字符串
    cJSON *root_abj = cJSON_CreateObject();
    if (root_abj == NULL)
    {
        log_error("create root_abj failed");
        return -1;
    }
    
    //添加name和age
    cJSON_AddStringToObject(root_abj, "name", name);
    cJSON_AddNumberToObject(root_abj, "age", age);
    //生成json字符串
    char *json_str = cJSON_PrintUnformatted(root_abj);
    log_debug("json_str: %s", json_str);

    //解析json字符串
    cJSON *root = cJSON_Parse(json_str);
    if (root == NULL)
    {
        log_error("parse json_str failed");
        return -1;
    }

    //获取name和age
    cJSON *name_str = cJSON_GetObjectItem(root, "name");
    cJSON * age_num = cJSON_GetObjectItem(root, "age");
    if (name_str == NULL || age_num == NULL)
    {
        log_error("get name or age failed");
        return -1;
    }
    char *name_str_val = name_str->valuestring;
    int age_num_val = age_num->valueint;
    log_debug("name: %s, age: %d", name_str_val, age_num_val);

    //释放内存
    cJSON_Delete(root);
    cJSON_Delete(root_abj);
    

    return 0;
}
